package com.ly.hotel.view.fragments.vod;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.Toast;

import androidx.viewpager.widget.PagerAdapter;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SharedPreferencesConfig;
import com.ly.hotel.model.Vodvideo;
import com.ly.hotel.util.ToastUtil;
import com.ly.hotel.view.common.CloudAdShowActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZHONGYUAN on 2018/1/3.
 */

public class EpisodePagerAdapter extends PagerAdapter {
	
	private List<List<Vodvideo>> shopIntroduce;
    private Context mContext;
    private String tmp[];

    public EpisodePagerAdapter(List<List<Vodvideo>> lv, Context context){
        shopIntroduce = lv;
        mContext = context;
        tmp =  MyApplication.getHostIp().split(":");
    }

    @Override
    public int getCount() {
        return shopIntroduce.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

	@Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.episode_pager_item,container,false);
        GridLayout gdl = (GridLayout) view.findViewById(R.id.episodes_gridlayout);
        int marginX = mContext.getResources().getDimensionPixelSize(R.dimen.px16);
        int marginY = mContext.getResources().getDimensionPixelSize(R.dimen.px10);
   		int btnHeight = mContext.getResources().getDimensionPixelSize(R.dimen.px106);
   		int btnWidth = mContext.getResources().getDimensionPixelSize(R.dimen.px160);
   		List<Vodvideo> vvl = new ArrayList<Vodvideo>();
   		vvl = shopIntroduce.get(position);
   		for (final Vodvideo vv:vvl) {
   			Button tmpBtn = new Button(mContext);
   			String name = vv.getName();
   			tmpBtn.setText(name);
   			tmpBtn.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.act_keyboard_btn));
   			tmpBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.getResources().getDimensionPixelSize(R.dimen.px32));
   			GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
   			layoutParams.width = btnWidth;
   			layoutParams.height = btnHeight;
   			layoutParams.setMargins(marginX,marginY,marginX,marginY);
   			tmpBtn.setLayoutParams(layoutParams);			
   			gdl.addView(tmpBtn);
   			tmpBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					play(vv);
				}
			});
   		}
        container.addView(view);
        return view;
    }
	
	private void play(Vodvideo v) {
		String url = v.getPath();
		Log.d("apaclick", "将要播放" + v.getName());
		if(MyApplication.licenseTimeOut && MyApplication.ukeyInvalidDays >= 0){
			//U盾过去，过去时间一天
			if(MyApplication.ukeyInvalidDays > 0){
				//U盾过去，过去时间一天以上
				Log.i("apaclick", "Ukey的过期了，禁止看电影!");
			} else if(MyApplication.startUpTimeHour >= 14){
				Log.i("apaclick", "Ukey的过期了，禁止看电影!");
			}
		} else if(!SharedPreferencesConfig.getMovieEnable(mContext)){
			ToastUtil.showShortToast(mContext, "尊敬的贵宾:本区域暂不支持视频播放功能", Toast.LENGTH_LONG);
		} else {	
			if (url != null) {
				
				try {
					//由于空格转码后为+号，所以替换+号成%20,"/"不需要转码所以还原
					url = URLEncoder.encode(url, "utf-8").replaceAll("\\+","%20").replaceAll("%2F","/");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
				if(tmp.length == 2 && !v.getServerPort().equals("0")){
					url = "http://" + tmp[0] + ":" + v.getServerPort() + "/" + url;
				}else{
					url = MyApplication.getHostResUrl() + url;
				}
					
				//调用系统自带的播放器   
					    
				Intent intent = new Intent(mContext, CloudAdShowActivity.class);
				intent.putExtra("actionType", "vod");
				intent.putExtra("path", url);
				intent.putExtra("filmName", v.getName());
				intent.putExtra("position", v.getPlayPosition());
				intent.putExtra("filmId", v.getId());
				mContext.startActivity(intent);
			} else {
				Toast.makeText(mContext, "没有找到该视频", Toast.LENGTH_SHORT).show();
			}
		}
	}

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
